%% Author: zhangjun
%% Created: 2008-11-23
%% Description: TODO: Add description to erltest2
-module(erltest2).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([start/3]).

%%
%% API Functions
%%

%%
%% TODO: Add description of start/function_arity
%%
start(N, M, Message) -> 
	Pid = create(N,0),
    send(M,Pid,Message),
    ok.

send(M,Pid,Message) ->
    case M of 
   		0 ->
            ok;
        _ ->
        	Pid ! Message,
            send(M-1, Pid, Message)
    end.

create(N, Spid) ->
    case N of
    	0 ->
        	Spid;
        _ ->
			Pid = spawn(fun() -> loop(Spid) end),
            create(N-1, Pid)
    end.   

loop(Spid) ->
    receive
	Any ->
	    io:format("~p Received:~p~n",[self(),Any]),
        case Spid of
            0 ->
                ok;
            _ ->
                Spid ! Any
        end,
	    loop(Spid)
    end.
        
%%
%% Local Functions
%%

